package com.android.tiango.baba.act;

import java.io.File;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaListActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.db.DbHelper;
import com.android.tiango.baba.entity.OrderEntity;
import com.android.tiango.baba.entity.OrderModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.TimeToUtil;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.CountdownView;
import com.android.tiango.baba.view.PullListView.PullListViewListener;

public class ActMyBaoBeiList extends BaListActivity<OrderModel> implements PullListViewListener {

	LayoutInflater mInflater;
	private ArrayList<OrderModel> mOrderList;
	private DbHelper helper;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
	}
	private void initView() {
		// TODO Auto-generated method stub
		setTitleName("我的宝贝");
		mTitleBar.setBackIcon(R.drawable.back);
		setBackOnClickListener();
		mInflater=getLayoutInflater();
		mListView.setDividerHeight(10);
		mListView.setPullListener(this);
		mListView.startOnRefresh();
	}
	
	@Override
	public void onPullRefresh() {
		mPageIndex = 1;
		onLoadData();
	}

	@Override
	public void onPullLoadMore() {
		mPageIndex += 1;
		onLoadData();

	}
	private Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			
			onLoadData();
			super.handleMessage(msg);
			
		}
	};
	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		
		viewHolder viewholder;
		if(convertView==null){
			viewholder=new viewHolder();
			convertView=mInflater.inflate(R.layout.item_my_baobei, null);
			viewholder.name=(TextView) convertView.findViewById(R.id.tv_name);
			viewholder.baobei=(ImageView) convertView.findViewById(R.id.iv_baobei);
			viewholder.data=(TextView) convertView.findViewById(R.id.tv_data);
			
			viewholder.picture=(ImageView) convertView.findViewById(R.id.iv_picture);
			viewholder.style=(Button) convertView.findViewById(R.id.bt_style);
			viewholder.mTime=(CountdownView) convertView.findViewById(R.id.countdownView1);
			
			convertView.setTag(viewholder);
		}else{
			viewholder=(viewHolder) convertView.getTag();
		}
		
		OrderModel bean=mAdapter.getItem(position);
		String type=bean.getOrderStatusType();
		
		String name=bean.getSimpleProduct().getProductName();
		String url=bean.getSimpleProduct().getProductImage().getImageUrl();
		String confirmTime=bean.getConfirmTime();
		String creatTime=TimeToUtil.getTimeSetFormat(Long.valueOf(bean.getCreateTime()));
		String orderId=bean.getOrderId();
		String expressNO=bean.getExpressNO();
		viewholder.style.setTag(bean);
		Uri Imageuri=Uri.fromFile(new File(url));
		if("0".equals(type)){//未提交
			viewholder.name.setText(name);
			viewholder.baobei.setImageURI(Imageuri);
			viewholder.data.setText(creatTime);
			viewholder.picture.setImageResource(R.drawable.baobei_confirm);
			viewholder.style.setText("给我发货");
			viewholder.mTime.setVisibility(View.VISIBLE);
			viewholder.mTime.setTime(handler, Long.valueOf(confirmTime));
			viewholder.style.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					showActivity(ActConfirm.class, false);
				}
			});
			
		}else if("1".equals(type)){//待支付
			
		}else if("2".equals(type)){//待发货
			viewholder.name.setText(name);
			viewholder.baobei.setImageURI(Imageuri);
			viewholder.data.setText(creatTime);
			viewholder.picture.setImageResource(R.drawable.baobei_send);
			viewholder.style.setText("提醒发货");
			viewholder.mTime.setVisibility(View.INVISIBLE);
			viewholder.style.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					showExitGameAlert();					
				}
			});
		}else if("3".equals(type)){//已发货
			viewholder.name.setText(name);
			viewholder.baobei.setImageURI(Imageuri);
			viewholder.data.setText(creatTime);
			viewholder.picture.setImageResource(R.drawable.baobei_sended);
			viewholder.style.setText("查询物流");
			viewholder.mTime.setVisibility(View.INVISIBLE);
			viewholder.style.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					showActivity(ActLogisticList.class, false);
				}
			});
			
		}else if("4".equals(type)){//已签收
			viewholder.name.setText(name);
			viewholder.baobei.setImageURI(Imageuri);
			viewholder.data.setText(creatTime);
			viewholder.picture.setImageResource(R.drawable.baobei_sign);
			viewholder.style.setText("晒单");
			viewholder.mTime.setVisibility(View.INVISIBLE);
			viewholder.style.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					OrderModel newbean=	(OrderModel) v.getTag();
					Intent intent=new Intent(ActMyBaoBeiList.this,ActShareGoods.class);
					Bundle bundle=new Bundle();
					bundle.putSerializable(Constants.DATA, newbean);
					intent.putExtras(bundle);
					startActivity(intent);
					
				}
			});
		}else if("5".equals(type)){//已完成
			viewholder.name.setText(name);
			viewholder.baobei.setImageURI(Imageuri);
			viewholder.data.setText(creatTime);
			viewholder.picture.setImageResource(R.drawable.baobei_sign);
			viewholder.style.setText("删除订单");
			viewholder.mTime.setVisibility(View.INVISIBLE);
			viewholder.style.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					OrderModel newbean=	(OrderModel) v.getTag();
					deleteData(newbean.getOrderId());
				}
			});
		}else if("6".equals(type)){//关闭（作废）
			
		}else if("99".equals(type)){//历史
			
		}
		
		/*if(position==2){
			
		}else if(position==3){
			viewholder.name.setText("澳洲桑葚果肉果冻");
			viewholder.baobei.setImageResource(R.drawable.baobei_chanpin);
			viewholder.data.setText("2014-8-29");
			viewholder.picture.setImageResource(R.drawable.baobei_sended);
			viewholder.style.setText("查询物流");
			viewholder.mTime.setVisibility(View.INVISIBLE);
			viewholder.style.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					showActivity(ActLogisticList.class, false);
				}
			});
		}else{
			
			viewholder.name.setText("澳洲桑葚果肉果冻");
			viewholder.baobei.setImageResource(R.drawable.baobei_chanpin);
			viewholder.data.setText("2014-8-29");
			viewholder.mTime.setVisibility(View.INVISIBLE);
			viewholder.picture.setImageResource(R.drawable.baobei_confirm);
			
			viewholder.style.setText("给我发货");
			
		}
		
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});*/
		return convertView;
	}
	class viewHolder{
		private TextView name,data;
		private Button style;
		private ImageView baobei,picture;
		private CountdownView mTime;
		
	}
	
	private void showExitGameAlert() {
		 final AlertDialog dlg = new AlertDialog.Builder(this).create();
		 dlg.show();
		 Window window = dlg.getWindow();
		        // *** 主要就是在这里实现这种效果的.
		        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
		 window.setContentView(R.layout.layout_send_remind);
		        // 为确认按钮添加事件,执行退出应用操作
		
	}
	
	//删除订单
	private void deleteData(String orderId){
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams httpparams = new HttpParams();
		httpparams.put("token", ShareCookie.getToken());//taken
		httpparams.put("orderId", orderId);
		

		client.post(HttpUrl.getUrl(HttpUrl.DELETEMYORDER), httpparams,
				new HttpCallBack() {
					@Override
					public void onHttpSuccess(Object arg0) {
						// TODO Auto-generated method stub
						
						ToastUtils.showMessage("成功！！");
					}

					@Override
					public void onHttpStarted() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						
						ToastUtils.showMessage("宝贝获取失败！===" + arg1);

						arg0.printStackTrace();
						
						
					}
				});
	}
	//加载宝贝
	private void onLoadData() {
		// TODO Auto-generated method stub
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams httpparams = new HttpParams();
		httpparams.put("token", ShareCookie.getToken());//taken
		httpparams.put("pageIndex", mPageIndex);
		httpparams.put("pageSize", 	Constants.PAGE_SIZE);

		client.get(HttpUrl.getUrl(HttpUrl.MYGOODS), httpparams,
				new HttpCallBack() {
					@Override
					public void onHttpSuccess(Object arg0) {
						// TODO Auto-generated method stub
						OrderEntity orderentity=(OrderEntity) arg0;
						mOrderList=orderentity.getData();
						if(null!=mOrderList){
							mAdapter.putData(mOrderList);
							
						}
						
						mListView.onRefreshFinish();
						mListView.onLoadFinish();
						
						ToastUtils.showMessage("成功！！");
					}

					@Override
					public void onHttpStarted() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						
						ToastUtils.showMessage("宝贝获取失败！===" + arg1);
						if(mPageIndex>1){
							mPageIndex-=1;
						}
						arg0.printStackTrace();
						
						mListView.onRefreshFinish();
						mListView.onLoadFinish();
					}
				},OrderEntity.class);
	}
}
